﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
namespace WPFExcelReport
{
    class JLTGB33Report : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        string ABPAUFFKRS;
        string ABPAUFFLFD;
        string ABPAUFFPOS;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string strExcelFile;
        Dictionary<string, int> qpplan = new Dictionary<string, int>();
        
        #endregion
        public JLTGB33Report()
            : base()
        {
        }
        public JLTGB33Report(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            if (dicParamater.ContainsKey("ROOTDRIVE"))
            {
                Root = dicParamater["ROOTDRIVE"].ToString();
            }
            else
            {
                Root = "C:\\IntexReport";
            }
                      						

        } 
        #region------------------------------Function------------------------
        /*ORDERNO	SUPP SUPPNAME	EKKBDAT		ARTICLE	QTY	INQTY	GPEINDAT	PRICE	SCHEDNO
         LOT	SPART	AMM	BMM	CMM	DMM	AVCN	DIFCN	MINCN	CV	THINA	THINB	THICKA	
                    THICKB	NEPSA	NEPSB	HAIR	UINDEX	TITER	ENDB	ENDH	REMARK*/
        public void FillInExcelData(DataSet setSoruce, int xStart = 2, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["GB33"];
                DataTable dtOutput = new DataTable();
                dtOutput.Columns.Add("ARTICLE", typeof(System.String));
                dtOutput.Columns.Add("GSHANDLNR", typeof(System.String));
                dtOutput.Columns.Add("SUPP", typeof(System.String));
                dtOutput.Columns.Add("SUPPNAME", typeof(System.String));
                dtOutput.Columns.Add("GPEINDAT", typeof(System.String));
                dtOutput.Columns.Add("INQTY", typeof(System.String));
                dtOutput.Columns.Add("LOT", typeof(System.String));
                dtOutput.Columns.Add("SPART", typeof(System.String));
                dtOutput.Columns.Add("AMM", typeof(System.String));
                dtOutput.Columns.Add("BMM", typeof(System.String));
                dtOutput.Columns.Add("CMM", typeof(System.String));
                dtOutput.Columns.Add("DMM", typeof(System.String));
                dtOutput.Columns.Add("AVCN", typeof(System.String));
                dtOutput.Columns.Add("DIFCN", typeof(System.String));
                dtOutput.Columns.Add("MINCN", typeof(System.String));
                dtOutput.Columns.Add("CV", typeof(System.String));
                dtOutput.Columns.Add("THINA", typeof(System.String));
                dtOutput.Columns.Add("THINB", typeof(System.String));
                dtOutput.Columns.Add("THICKA", typeof(System.String));
                dtOutput.Columns.Add("THICKB", typeof(System.String));
                dtOutput.Columns.Add("NEPSA", typeof(System.String));
                dtOutput.Columns.Add("NEPSB", typeof(System.String));
                dtOutput.Columns.Add("HAIR", typeof(System.String));
                dtOutput.Columns.Add("UINDEX", typeof(System.String));
                dtOutput.Columns.Add("TITER", typeof(System.String));
                dtOutput.Columns.Add("ENDB", typeof(System.String));
                dtOutput.Columns.Add("ENDH", typeof(System.String));
                dtOutput.Columns.Add("REMARK", typeof(System.String));
                dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);

                exAgent.DataTableToExcel(dtOutput, 2, 1);

                int count = dtOutput.Rows.Count;
                exAgent.SetCellValue(1, 1, 1);
                exAgent.BatchConvert2Number(2, 3, count + 1, 3);
                exAgent.BatchConvert2Number(2, 6, count + 1, 7);
                exAgent.SetCellValue(1, 1, "纱线编号");
                						
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }        
        public override void WriteExcelReport(string fileType)
        {
            bool IsFTP = false;
            try
            {
                strCompany = "JLT";
                strExcelFile = @"JLT\JLTReport.xls";
                Standard = "GB33";
                TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\JLT\JLTReport.xls");
                destfolder = SetFolder(Root, "采购部");
                string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                string newfilename = @"坯纱质量" + filedate;
                
                exAgent.CheckWorkBook(newfilename);
                ServerShareFile("", year, month, newfilename, fileType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void ServerShareFile(string KRS, string year, string month, string ordernumer, string fileType)
        {
            string destfile, tempfile = "";
            string fileabsname = ordernumer;
            try
            {
                filename = destfolder + "\\" + fileabsname + ".xls"; ;
                destfile = filename;
                tempfile = CreateFile(ordernumer);
                if (File.Exists(destfile))
                {
                    File.Delete(destfile);
                }
                File.Move(tempfile, destfile);

                if (fileType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (File.Exists(tempfile))
                {
                    File.Delete(tempfile);
                }
            }
        }
        private string CreateFile(string orderno)
        {
            string filename = "";
            try
            {
                string pwd = "";
                string sheetName = orderno;
                exAgent.Open(TemplatePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                exAgent.CreateNewSheet(orderno + ".xls", Standard, sheetName, pwd);
                filename = exAgent.FileName;
                exAgent.SetCurrentSheet(1);
                FillInExcelData(dsSource, 2, 1);
                exAgent.Save();
                exAgent.Close();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return filename;
        }
        #endregion
    }
}